const fs = window.require('fs')
const fileHelper = {
    //读取
    readFile: (path: string) => {
        return fs.readFile(path, { encoding: 'utf8' })
    },
    //写入
    writeFile: (path: string, content: string) => {
        return fs.writeFile(path, content, { encoding: 'utf8' })
    },
    //重名
    renameFile: (path: string, newPath: string) => {
        return fs.rename(path, newPath)
    },
}

export default fileHelper

//文件删除
export function deleteFolder(path: string) {
    let files = [];
    if (fs.statSync(path).isDirectory()) {
        if (fs.existsSync(path)) {
            files = fs.readdirSync(path);
            files.forEach(function (file: string, index: number) {
                let curPath = path + "/" + file;
                if (fs.statSync(curPath).isDirectory()) {
                    deleteFolder(curPath);
                } else {
                    fs.unlinkSync(curPath);
                }
            });
            fs.rmdirSync(path);
        }
    } else {
        fs.unlinkSync(path);
    }
    return true
}
